<template>
  <div>
    <div>【关键词条】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'
import 'echarts-wordcloud'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

// 获取 dom 实例
const target = ref(null)
let myChart = null
const randomRGB = () => {
  const r = Math.floor(Math.random() * 255)
  const g = Math.floor(Math.random() * 255)
  const b = Math.floor(Math.random() * 255)
  return `rgb(${r}, ${g}, ${b})`
}
const render = () => {
  const option = {
    series: {
      type: 'wordCloud',
      data: props.data.datas,
      sizeRange: [8, 46],
      rotationRange: [0, 0],
      layoutAnimation: true,
      textStyle: {
        color: randomRGB
      },
      emphasis: {
        textStyle: {
          fontWeight: 'bold',
          color: '#000'
        }
      }
    }
  }
  myChart.setOption(option)
}
onMounted(() => {
  myChart = echarts.init(target.value)
  render()
})
watch(
  () => props.data,
  (newValue) => {
    render()
  }
)
</script>

<style lang="scss" scoped></style>
